package com.cgbl.service.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ktx.immersionBar
import com.just.agentweb.AgentWeb
import com.robot.base.view.webview.WebLayout

class WebActivity : AppCompatActivity() {

    private lateinit var agentWeb: AgentWeb

    private var mUrl = ""
    private var title = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val webLayout = WebLayout(this)
        setContentView(webLayout.layout)
        immersionBar {
            transparentBar()
//            keyboardEnable(true)
            fitsSystemWindows(false)
            statusBarDarkFont(true)
        }

        if (intent != null) {
            mUrl = intent.getStringExtra(EXTRA_URL) ?: ""
            title = intent.getStringExtra(EXTRA_TITLE) ?: ""
        }
//        val tvTitle = webLay/out.layout.findViewById<AppCompatTextView>(R.id.tv_title)
//        tvTitle.text = title
//        webLayout.layout.findViewById<AppCompatImageView>(R.id.iv_back).setOnClickListener {
//            finish()
//        }
//        mUrl = "https://www.baidu.com"
        agentWeb = AgentWeb.with(this)
            .setAgentWebParent(webLayout.webView!!, LinearLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .createAgentWeb()
            .ready()
            .go(mUrl)
    }

    private fun load(view: WebView?, url: String) {
        view?.loadUrl(url)
    }

    companion object {
        const val EXTRA_URL = "url"
        const val EXTRA_TITLE = "title"

        fun start(ctx: Context, url: String, title: String) {
            Log.d("WebService", "url-$url")
            val intent = Intent(ctx, WebActivity::class.java)
            intent.putExtra(EXTRA_URL, url)
            intent.putExtra(EXTRA_TITLE, title)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            ctx.startActivity(intent)
        }
    }
}